package com.leehom.arch.obs.metrics.exporter.prometheus;

import java.io.IOException;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
import com.leehom.arch.obs.metrics.exporter.MetricType;

/**
 * @类名: DropWizardPrometheusExporter
 * @说明: Dropwizard Metrics输出(转换)为Prometheus metrics
 *     
 *
 * @author   leehom
 * @Date	 2021年12月2日 下午5:44:57
 * 修改记录：
 *
 * @see 	 
 */
public class DropWizardPrometheusExporter {
	
    public io.prometheus.client.Gauge exportGauge(String dropwizardName, String help, Gauge<?> gauge) {
    	String name = sanitizeMetricName(dropwizardName);
    	io.prometheus.client.Gauge promGauge = io.prometheus.client.Gauge.build(name, help).create(); 
    	Object obj = gauge.getValue();
        double value;
        if (obj instanceof Number) {
            value = ((Number) obj).doubleValue();
            promGauge.set(value);
        }  else {
        	return null;
        }
        return promGauge;
    }

    public io.prometheus.client.Counter exportCounter(String dropwizardName, String help, Counter counter) {
        String name = sanitizeMetricName(dropwizardName);
        io.prometheus.client.Counter promCounter = io.prometheus.client.Counter.build(name, help).create(); 
        promCounter.inc(counter.getCount());
        return promCounter;

    }
    
    // export as counter
    public io.prometheus.client.Counter exportMeter(String name, Meter meter) {
        return null;
    }

    /**
     * Export a histogram snapshot as a prometheus SUMMARY.
     *
     * @param dropwizardName metric name.
     * @param snapshot the histogram snapshot.
     * @param count the total sample count for this snapshot.
     * @param factor a factor to apply to histogram values.
     * @throws IOException
     *
     */
    public io.prometheus.client.Summary exporterHistogram(String dropwizardName, Histogram histogram) {
        return exportSnapshotAndCount(dropwizardName, histogram.getSnapshot(), histogram.getCount(), 1.0, 
        			MetricType.SUMMARY, getHelpMessage(dropwizardName, histogram));
    }

    private io.prometheus.client.Summary exportSnapshotAndCount(String dropwizardName, Snapshot snapshot, 
    				long count, double factor, MetricType type, String helpMessage) {
        String name = sanitizeMetricName(dropwizardName);
        return null;
    }

    public io.prometheus.client.Histogram exportTimer(String dropwizardName, Timer timer) {
    	return null;
    }

    private String getHelpMessage(String metricName, Metric metric) {
        return String.format("Generated from Dropwizard metric import (metric=%s, type=%s)", metricName, metric.getClass().getName());
    }

    // group.tag...(0..*)
    private String sanitizeMetricName(String dropwizardName) {
    	if(dropwizardName.indexOf(".")>0)
    		return dropwizardName.substring(0, dropwizardName.indexOf("."));
    	else
    		return dropwizardName;
    }
}
